// pages/myCard/myCard.js
import {api} from "../../utils/api.js";
const app = getApp();
let paramsData = [];
let _select_idx = []; // 存放激活类下标
let _select_state = [
  false,
  false,
  false,
  false,
  false
];

Page({
  data: {
    draw_residue_num: 99, // 99占位,在请求未完成之前占位
    user_card_list: [],
    youUrl: "http://zhao810.gitee.io/e-images/home/redPost.png",
    youSelectedUrl: "http://zhao810.gitee.io/e-images/home/yellowPost.png",
    select_state: [
      false,
      false,
      false,
      false,
      false
    ]
  },

  onLoad: function (options) {
    this.updateData()
  },
  selectCard(options) {
    // 判断用户卡片数量是否足够
    let cardNum = options.currentTarget.dataset.cardNum;
    if (!cardNum) {
      wx.showToast({
        title: '该卡片数量不足!',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let idx = options.currentTarget.dataset.idx;
    // 判断对应元素的选中状态，取反操作
    let val = this.data.select_state[idx] == true ? false : true;
    if (val) {
      _select_idx.push(idx);
      _select_idx = _select_idx.splice(_select_idx.length - 2);
    } else {
      _select_idx = _select_idx.filter(e => e != idx);
    }
    // 将所有元素的选中状态清空/重置为false
    _select_state.forEach((item, index) => {
      _select_state[index] = false;
    })
    // 将被选中元素，状态设置为true
    _select_idx.forEach((item, idx) => {
      _select_state[item] = true;
    })

    this.setData({
      select_state: _select_state
    });
    // 修改提交的卡片id
    this.setParam(options.currentTarget.dataset.cardId, val);
  },
  setParam(id, val) {
    if (val) {
      paramsData.push(id);
      paramsData = paramsData.splice(paramsData.length - 2);
    } else {
      paramsData = paramsData.filter(e => e != id);
    }
    // console.log(paramsData);
  },
  lotto() {
    // console.log(paramsData.length);//判断卡片数量，达到2张才能抽奖
    if (paramsData.length == 2){
      wx.request({
        url: api.draw,
        data: {
          access_token: app.globalData.access_token,
          card_type_id1: paramsData[0],
          card_type_id2: paramsData[1]
        },
        success: (result) => {
          // console.log("抽奖结果", result)
          // 如果用户选中的卡片数量=0,弹窗提示用户重新选择卡片
          // 抽奖完成,更新页面卡片数量
          this.updateData()
          if (result.data.data.prize_name == "未中奖"){
            wx.showToast({
              title: result.data.data.prize_name,
              icon: 'none',
              duration: 2000
            })
          }else{
            wx.showToast({
              title: result.data.data.prize_name,
              icon: 'success',
              duration: 2000
            })
          }
        }
      });
    }else{
      wx.showToast({
        title: '请选择两张邮卡',
        icon: 'none',
        duration: 2000
      })
    }  
  },
  updateData() {
    wx.request({
      url: api.myCard,
      data: {
        access_token: app.globalData.access_token
      },
      success: (res) => {
        // console.log("用户个人数据", res.data.data.user_card_list)
        this.setData({
          draw_residue_num: res.data.data.draw_residue_num,
          user_card_list: res.data.data.user_card_list
        })
      }
    });
  },
  //页面隐藏时，清除选中状态
  onUnload(){
    paramsData = [];
    _select_idx = []; // 存放激活类下标
    _select_state = [
      false,
      false,
      false,
      false,
      false
    ];
  }
})